import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';
interface Message {
	filter: {
		teacher_no: string;
	};
	productid: string;
	page_no: number;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
interface Task {
	create_time: number;
	create_time_str: string;
	personalized_setting: string;
}
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-arrange-homework/s001,action_id:' + action_id);

	const { prepare, exec } = await query(action_id, session_id, systemid);

	prepare('task', ['job_no', 'quote_job_name', 'contents_homework', 'personalized_setting', 'create_time'], { teacher_no: message.filter.teacher_no, productid: message.productid }, 20, message.page_no, [], ['create_time']);

	const [data] = await exec<Task>();

	data.forEach((v) => {
		v.create_time_str = format(v.create_time, 'YYYY/MM/DD');
		if (v.personalized_setting === '0') {
			v.personalized_setting = '全班';
		} else {
			v.personalized_setting = '个人';
		}
	});

	log('Service end path:app-arrange-homework/s001,action_id:' + action_id);
	return {
		data
	};
}
